﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Manazra.Models;
using facebook;

namespace Manazra.Helpers
{
    public class FBPublisher
    {
        private const string siteUrl = "http://manazra.com/";

        private ManazraDataContext dc = new ManazraDataContext();

        //publishes the new article posted news to facebook user wall and their friends wall
        public bool PublishNewArticleFeed(UserAuth UserAuth, Article article)
        {
            //make sure user has given us auto publish permissions
            if (IsPermitted(UserAuth))
            {
                //load facebook api using current session
                API api = UserAuth.LoadFBApi();

                //setup template data
                Dictionary<string, string> TemplateData = new Dictionary<string, string>();

                TemplateData.Add("site-url", siteUrl);
                //format article title
                string articleTitle = article.Title.Replace("\"", "").Replace("\\", "").Replace("\r\n","");
                TemplateData.Add("article-title", articleTitle);

                //format article url
                string articleUrl = siteUrl + "Articles/Details/" + article.RecId + "_" + articleTitle.Replace(' ', '_');
                TemplateData.Add("article-url", articleUrl);

                //format article intro string
                string articleIntro = article.Intro;
                if (articleIntro.Length > 200)
                    articleIntro = articleIntro.Substring(0, 200);
                articleIntro = articleIntro.Replace("\"", "").Replace("\\", "").Replace("\r\n", "");
                TemplateData.Add("article-intro", articleIntro);

                //send feed to facebook
                try
                {
                    api.feed.publishUserAction(110995214835, TemplateData, null, "", feed.PublishedStorySize.Short);
                    return true;
                }
                catch
                {
                    //ignore exception 
                    return false;
                }
            }
            return false;
        }

        //publishes new comment posted news to facebook
        public bool PublishNewCommentFeed(UserAuth UserAuth, Comment Comment)
        {
            //make sure user has given us auto publish permissions
            if (IsPermitted(UserAuth))
            {
                //load facebook api using current session
                API api = UserAuth.LoadFBApi();
                Article article = Comment.ArticleSegment.Article;

                //setup template data
                Dictionary<string, string> TemplateData = new Dictionary<string, string>();

                TemplateData.Add("site-url", siteUrl);
                //format article title
                string articleTitle = article.Title.Replace("\"", "").Replace("\\", "").Replace("\r\n", "");
                TemplateData.Add("article-title", articleTitle);

                //format article url
                string articleUrl = siteUrl + "Articles/Details/" + article.RecId + "_" + articleTitle.Replace(' ', '_');
                TemplateData.Add("article-url", articleUrl);

                //format comment string
                string comment = Comment.Content;
                if (comment.Length > 200)
                    comment = comment.Substring(0, 200);
                TemplateData.Add("comment", comment.Replace("\"", "").Replace("\\", "").Replace("\r\n", ""));

                //send feed to facebook
                try
                {
                    api.feed.publishUserAction(110995654835, TemplateData, null, "", feed.PublishedStorySize.Short);
                    return true;
                }
                catch
                {
                    //ignore exception 
                    return false;
                }
            }
            return false;
        }

        //publishes new reply posted news to facebook
        public bool PublishNewReplyFeed(UserAuth UserAuth, Reply Reply)
        {
            //make sure user has given us auto publish permissions
            if (IsPermitted(UserAuth))
            {
                //load facebook api using current session
                API api = UserAuth.LoadFBApi();
                Article article = Reply.Comment.ArticleSegment.Article;

                //setup template data
                Dictionary<string, string> TemplateData = new Dictionary<string, string>();

                TemplateData.Add("site-url", siteUrl);
                //format article title
                string articleTitle = article.Title.Replace("\"", "").Replace("\\", "").Replace("\r\n", "");
                TemplateData.Add("article-title", articleTitle);

                //format article url
                string articleUrl = siteUrl + "Articles/Details/" + article.RecId + "_" + articleTitle.Replace(' ', '_');
                TemplateData.Add("article-url", articleUrl);

                //format comment string
                string reply = Reply.Content;
                if (reply.Length > 200)
                    reply = reply.Substring(0, 200);
                TemplateData.Add("comment", reply.Replace("\"", "").Replace("\\", "").Replace("\r\n", ""));

                //send feed to facebook
                try
                {
                    api.feed.publishUserAction(110995654835, TemplateData, null, "", feed.PublishedStorySize.Short);
                    return true;
                }
                catch
                {
                    //ignore exception 
                    return false;
                }
            }
            return false;
        }

        //checks publish permissions
        private bool IsPermitted(UserAuth UserAuth)
        {
            try
            {
                //check currently logged in users facebook permission information
                FacebookUser fbUser = dc.FacebookUsers.Single(u => u.UserId == UserAuth.UserID);
                dc.Log = new DebuggerWriter();
                return (fbUser.PermissionStatus.IndexOf("publish_stream") != -1);
            }
            catch
            {
                return false;
            }
        }
    }
}
